cd ""
use appended1to4_recode.dta, clear
*keep if mean_polsoph une_chng_CSES_precrisisCSES>-8 
log using log_files\15022017.log, replace

gen A2001_sq = A2001*A2001
recode male (1=0) (0=1), gen(female)

*With mean_polsoph une_chng_CSES_2006: unemployment change between post-crisis 
	*wave of the CSES and 2006 (marks pre-crisis for all countries)
xtmixed pol_soph mean_polsoph une_chng_CSES_2006 female A2001 A2001_sq bachelor  ///
	no_labor_force student part_time unempl i.hh_type i.A1008 if non_EU==0 || A1006: , mle var
eststo M101 

	/* 
	set level 90
	margins , at(une_chng_CSES_2006=(-5(1)9) female=1 bachelor=0 unempl=0 part_time=0 no_labor_force=0 mean_polsoph=8) atmeans
	marginsplot , x(une_chng_CSES_2006) xlabel(-5(5)10) ///
	ytitle("Predicted Political Knowldge") scheme(s2mono)
	*/

/*Testing for a curvilinear main effect	of change in unemployment:
*Using a quadratic term
gen une_chng_CSES_2006_2=une_chng_CSES_2006*une_chng_CSES_2006
xtmixed pol_soph A2001 A2001_sq  female bachelor unempl part_time no_labor_force student ///
	mean_polsoph une_chng_CSES_2006 une_chng_CSES_2006_2 C5085_2 i.A1008 if non_EU==0 || A1006: , mle var
*Using sample where unemployment increased
xtmixed pol_soph A2001 A2001_sq  female bachelor unempl part_time no_labor_force student ///
	mean_polsoph une_chng_CSES_2006 C5085_2 i.A1008 if une_chng_CSES_2006>0if non_EU==0 || A1006: , mle var
*Using sample where unemployment decreased
xtmixed pol_soph A2001 A2001_sq  female bachelor unempl part_time no_labor_force student ///
	mean_polsoph une_chng_CSES_2006 C5085_2 i.A1008 if une_chng_CSES_2006<0if non_EU==0 || A1006: , mle var
*/	
	
*Testing for an interaction between unemployment (change and absolute levels) 
	*and being unemployed	
xtmixed pol_soph mean_polsoph c.une_chng_CSES_2006##c.unempl female A2001  ///
	A2001_sq bachelor no_labor_force student part_time i.hh_type i.A1008 if non_EU==0 || A1006: ///
	unempl, mle var cov(unstructured)
eststo M102


/*	
margins , at(unempl=(0 1) une_chng_CSES_2006=(-5(1)5) ) atmeans
	marginsplot , x(une_chng_CSES_2006) xlabel(-5(5)5) ///
	ytitle("Predicted Political Knowldge") plot1opts(lpattern("l")) ///
	plot2opts(lpattern("-")) recast(connected) recastci(rline) scheme(s2mono)

margins, dydx(unempl) at(une_chng_CSES_2006=(-6(1)10)) vsquish 
marginsplot, yline(0) title("Conditional Marginal Effect of Unemployed") ///
	subtitle("on Predicted Electoral Knowledge, with 95% CIs") ///
	xtitle("{&Delta}Unemployment since t-1") ///
	ytitle("Marginal Effect of Unemployed") ///
	xlabel(-5(5)10) scheme(s2mono) 

*/	
esttab M101 M102 using "Table2_Text.rtf", ///
	cells(b(star fmt(3)) se(fmt(4))) stats(N, fmt(0)) legend ///
	label collabels(none) varlabels(_cons Constant) replace


*Baseline models; Examine residuals for outliers
xtmixed pol_soph if non_EU==0 || A1006: , mle variance
eststo M1
di 0.159 / (0.159 + 0.742)

xtmixed pol_soph mean_polsoph if non_EU==0 || A1006: , mle variance
eststo M2
di (0.159-0.027) / 0.159

preserve
drop if non_EU==1
xtmixed pol_soph mean_polsoph i.A1008 || A1006: , mle variance
eststo M3

predict u0, reffects
predict u0se, reses
egen pickone = tag(A1003)
sort u0
generate u0rank = sum(pickone)
sort A1003
list A1003 u0 u0se u0rank une_chng_CSES_2006 if pickone==1
serrbar u0 u0se u0rank if pickone==1, scale(1.96) yline(0) ///
	xtitle("Election (ranked)") ytitle("Residuals") scheme(s2mono)

list A1003 if u0<-.5 & pickone==1
	
esttab M1 M2 M3 using "Table8_Appendix.rtf", cells(b(star fmt(3)) se(fmt(4))) ///
	stats(N, fmt(0)) legend label collabels(none) varlabels(_cons Constant) replace 
restore

*******************************ROBUSTNESS CHECKS******************************
*1. Re-run models with absolute levels of unemployment, C5085_2.
xtmixed pol_soph mean_polsoph C5085_2 female A2001 A2001_sq bachelor  ///
	no_labor_force student part_time unempl i.hh_type i.A1008 if non_EU==0 || A1006: , mle var
eststo M201
xtmixed pol_soph mean_polsoph c.C5085_2##c.unempl female A2001 A2001_sq ///
	bachelor no_labor_force student part_time i.hh_type i.A1008 if non_EU==0 || A1006: ///
	unempl, mle var cov(unstructured)
eststo M202
/*	margins , at(unempl=(0 1) C5085_2=(2(1)14) ) atmeans
	marginsplot , x(C5085_2) xlabel(0(5)15) ///
	ytitle("Predicted Political Knowldge") plot1opts(lpattern("l")) ///
	plot2opts(lpattern("-")) recast(connected) recastci(rline) scheme(s2mono)*/
esttab M201 M202 using "Table9_appendix.rtf", ///
	cells(b(star fmt(3)) se(fmt(4))) stats(N, fmt(0)) legend ///
	label collabels(none) varlabels(_cons Constant) replace
	
	
*2. Re-run analysis using placement of old/existing parties only
*drop if A1003==27602013
*drop if mean_polsoph une_chng_CSES_2006==9.4
xtmixed pol_soph_old_parties mean_polsoph une_chng_CSES_2006 female A2001 A2001_sq ///
	bachelor no_labor_force student part_time unempl i.hh_type i.A1008 if non_EU==0 || A1006: , mle var
eststo M301

xtmixed pol_soph_old_parties mean_polsoph c.une_chng_CSES_2006##c.unempl female ///
	A2001 A2001_sq bachelor no_labor_force student part_time i.hh_type i.A1008 if non_EU==0 || A1006: ///
	unempl, mle var cov(unstructured)
eststo M302
esttab M301 M302 using "Table10_appendix.rtf", ///
	cells(b(star fmt(3)) se(fmt(4))) stats(N, fmt(0)) legend ///
	label collabels(none) varlabels(_cons Constant) replace
	
xtmixed pol_sof_new mean_polsoph une_chng_CSES_2006 female A2001 A2001_sq ///
	bachelor no_labor_force student part_time unempl i.hh_type i.A1008 if non_EU==0 || A1006: , mle var
eststo M301a

xtmixed pol_sof_new mean_polsoph c.une_chng_CSES_2006##c.unempl female ///
	A2001 A2001_sq bachelor no_labor_force student part_time i.hh_type i.A1008 if non_EU==0 || A1006: ///
	unempl, mle var cov(unstructured)
eststo M302a
esttab M301 M302 M301a M302a using "Table10new_appendix.rtf", ///
	cells(b(star fmt(3)) se(fmt(4))) stats(N, fmt(0)) legend ///
	label collabels(none) varlabels(_cons Constant) replace	
	
*3. Use lagged values of unemployment change (change between post-crisis wave
	*and pre-crisis wave of the CSES, both lagged to t-1): une_chng_CSES_precrisisCSES
xtmixed pol_soph mean_polsoph une_chng_CSES_precrisisCSES female A2001 A2001_sq ///
	bachelor no_labor_force student part_time unempl i.hh_type i.A1008 if non_EU==0 || A1006: , mle var
eststo M401
xtmixed pol_soph mean_polsoph c.une_chng_CSES_precrisisCSES##c.unempl female ///
	A2001 A2001_sq bachelor no_labor_force student part_time i.hh_type i.A1008 if non_EU==0 || A1006: ///
	unempl, mle var cov(unstructured)
eststo M402
esttab M401 M402 using "Table11_appendix.rtf", ///
	cells(b(star fmt(3)) se(fmt(4))) stats(N, fmt(0)) legend ///
	label collabels(none) varlabels(_cons Constant) replace
	

*4.	With control variables at election level
xtmixed pol_soph mean_polsoph une_chng_CSES_precrisisCSES female A2001 A2001_sq ///
	bachelor no_labor_force student part_time unempl i.hh_type ///
	lnenep pr_sm mj_sm bicameral compulsory	i.A1008 if non_EU==0 || A1006: , mle var
eststo M501 

xtmixed pol_soph mean_polsoph c.une_chng_CSES_precrisisCSES##c.unempl female ///
	A2001 A2001_sq bachelor no_labor_force student part_time i.hh_type ///
	lnenep pr_sm mj_sm bicameral compulsory	i.A1008 ///
	if non_EU==0 || A1006: unempl, mle var cov(unstructured)
eststo M502

esttab M501 M502 using "Table12_appendix.rtf", ///
	cells(b(star fmt(3)) se(fmt(4))) stats(N, fmt(0)) legend ///
	label collabels(none) varlabels(_cons Constant) replace

	
*5. With HH income instead of unemployment status
xtmixed pol_soph mean_polsoph une_chng_CSES_precrisisCSES female A2001 A2001_sq ///
	bachelor no_labor_force student part_time unempl A2012 i.hh_type i.A1008 if non_EU==0 || A1006: , mle var
eststo M501 

xtmixed pol_soph mean_polsoph c.une_chng_CSES_precrisisCSES##c.A2012 unempl female ///
	A2001 A2001_sq bachelor no_labor_force student part_time i.hh_type i.A1008 ///
	if non_EU==0 || A1006: A2012, mle var cov(unstructured)
eststo M502

esttab M501 M502 using "Table13_appendix.rtf", ///
	cells(b(star fmt(3)) se(fmt(4))) stats(N, fmt(0)) legend ///
	label collabels(none) varlabels(_cons Constant) replace
	
/* with HH income quartile as factor variable	
xtmixed pol_soph mean_polsoph une_chng_CSES_precrisisCSES female A2001 A2001_sq ///
	bachelor no_labor_force student part_time unempl i.A2012 i.hh_type ///
	i.A1008 if non_EU==0 || A1006: , mle var
eststo M601 

xtmixed pol_soph mean_polsoph c.une_chng_CSES_precrisisCSES##i.A2012 unempl female ///
	A2001 A2001_sq bachelor no_labor_force student part_time i.hh_type ///
	i.A1008 ///
	if non_EU==0 || A1006: R.A2012, mle var
eststo M602

esttab M601 M602 using "Table14_appendix.rtf", ///
	cells(b(star fmt(3)) se(fmt(4))) stats(N, fmt(0)) legend ///
	label collabels(none) varlabels(_cons Constant) replace	*/
	
/*6. Rerun models excluding the US 2008, 2012 elections NO NEED IN EU SAMPLE
*With mean_polsoph une_chng_CSES_2006: unemployment change between post-crisis 
	*wave of the CSES and 2006 (marks pre-crisis for all countries)
preserve
drop if A1003==84002008 | A1003==84002012
xtmixed pol_soph mean_polsoph une_chng_CSES_2006 female A2001 A2001_sq bachelor  ///
	no_labor_force student part_time unempl i.hh_type i.A1008 if non_EU==0 || A1006: , mle var
eststo M701 

xtmixed pol_soph mean_polsoph c.une_chng_CSES_2006##c.unempl female A2001  ///
	A2001_sq bachelor no_labor_force student part_time i.hh_type i.A1008 if non_EU==0 || A1006: ///
	unempl, mle var cov(unstructured)
eststo M702

esttab M701 M702 using "Table14_appendix.rtf", ///
	cells(b(star fmt(3)) se(fmt(4))) stats(N, fmt(0)) legend ///
	label collabels(none) varlabels(_cons Constant) replace	
restore
*/

/*7. Testing only for EU countries

xtmixed pol_soph mean_polsoph une_chng_CSES_2006 female A2001 A2001_sq bachelor  ///
	no_labor_force student part_time unempl i.hh_type i.A1008 if non_EU==0|| A1006: , mle var
eststo M801 

	margins , at(une_chng_CSES_2006=(-6(1)10)) atmeans
	marginsplot , x(une_chng_CSES_2006) xlabel(-5(5)10) ///
	xtitle("{&Delta}Unemployment since 2006") ///
	ytitle("Predicted Political Knowldge") scheme(s2mono) ///
	xlabel(-5(5)10)


xtmixed pol_soph mean_polsoph c.une_chng_CSES_2006##c.unempl female A2001  ///
	A2001_sq bachelor no_labor_force student part_time i.hh_type i.A1008 if non_EU==0 || A1006: ///
	unempl, mle var cov(unstructured)
eststo M802

margins, dydx(unempl) at(une_chng_CSES_2006=(-6(1)10)) vsquish 
marginsplot, yline(0) title("Conditional Marginal Effect of Unemployed") ///
	subtitle("on Predicted Electoral Knowledge, with 95% CIs") ///
	xtitle("{&Delta}Unemployment since 2006") ///
	ytitle("Marginal Effect of Unemployed") ///
	xlabel(-5(5)10) scheme(s2mono) 

esttab M801 M802 using "Table15_appendix.rtf", ///
	cells(b(star fmt(3)) se(fmt(4))) stats(N, fmt(0)) legend ///
	label collabels(none) varlabels(_cons Constant) replace	
restore*/



*8. Interacting unemployed with age - not stat. sig. at any values.
xtmixed pol_soph mean_polsoph une_chng_CSES_2006 female bachelor no_labor_force ///
	student part_time c.A2001##unempl i.hh_type i.A1008 if non_EU==0 || A1006: unempl, mle var

margins , at(unempl=(0 1) A2001=(20(1)80) ) atmeans
	marginsplot , x(A2001)  ///
	ytitle("Predicted Political Knowldge") plot1opts(lpattern("l")) ///
	plot2opts(lpattern("-")) recast(connected) recastci(rline) scheme(s2mono)
log c

*9. Controling for PID: (Two variables: voted for PM's party; and voted for a party in govt)
** with execptyr
do Do_files/incumb_PID.do
xtmixed pol_soph execptyr mean_polsoph une_chng_CSES_2006 female A2001 A2001_sq bachelor  ///
	no_labor_force student part_time unempl i.hh_type i.A1008 if non_EU==0 || A1006: , mle var
eststo m877
xtmixed pol_soph execptyr mean_polsoph c.une_chng_CSES_2006##c.unempl female A2001  ///
	A2001_sq bachelor no_labor_force student part_time i.hh_type i.A1008 if non_EU==0 || A1006: ///
	unempl, mle var cov(unstructured)
eststo m878

*with govptyr
xtmixed pol_soph govptyr mean_polsoph une_chng_CSES_2006 female A2001 A2001_sq bachelor  ///
	no_labor_force student part_time unempl i.hh_type i.A1008 if non_EU==0 || A1006: , mle var
eststo m879
xtmixed pol_soph govptyr mean_polsoph c.une_chng_CSES_2006##c.unempl female A2001  ///
	A2001_sq bachelor no_labor_force student part_time i.hh_type i.A1008 if non_EU==0 || A1006: ///
	unempl, mle var cov(unstructured)
eststo m880

esttab m877 m878 m879 m880 using "Table14_appendix.rtf", ///
	cells(b(star fmt(3)) se(fmt(4))) stats(N, fmt(0)) legend ///
	label collabels(none) varlabels(_cons Constant) replace	
